package com.hongye.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 聚合数据相关
 * @author Administrator
 *
 */
public class JuheUtils {
	
	public static Logger log = Logger.getLogger(JuheUtils.class);
	public static final String log_url = "http://images.juheapi.com/banklogo/";//银行logo地址
	public static final String bankcardsilk_url = "http://bankcardsilk.api.juhe.cn/bankcardsilk/query.php";//银行卡类别查询接口地址
	public static final String bankcardsilk_AppKey = "839322ed055756c25efae5a32a50f79c";//银行卡类别查询AppKey
	public static final String idcard_url = "http://op.juhe.cn/idcard/query";//身份证实名认证接口地址
	public static final String idcard_AppKey = "f881ca428a6f55ccc3a6a7265cfeb7fc";//身份证实名认证AppKey
	public static final String verifybankcard4_url = "http://v.juhe.cn/verifybankcard4/query";//银行卡四元素校验接口地址
	public static final String verifybankcard4_AppKey = "6f9ae0d5f0999aee8cf84de2e9f1a09e";//银行卡四元素校验AppKey
	
	
	
	
	public JuheUtils() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 银行卡类别查询
	 * @author lifuqiang
	 * 2017-2-17
	 * @param bankcards：银行卡号
	 * @return
	 * res：（0银行卡错误，1成功，2卡异常）
	 */
	public static Map<String, Object> bankcardsilk(String bankcards){
		Map<String, String> params = new HashMap<String, String>();//请求参数集合
		Map<String, Object> data = new HashMap<String, Object>();//响应参数集合
		Map<String, Object> map = new HashMap<String, Object>();//返回参数集合
		params.put("key", bankcardsilk_AppKey);//您申请的APPKEY
		params.put("num", bankcards);//银行卡号
		String result = HttpUtils.get(HttpUtils.Parameter(bankcardsilk_url, params));
		data = Utils.toMap(result);
		if(data.isEmpty()){
			map.put("res", "0");
			map.put("reason", "银行卡错误");
		}else if(data.get("reason").equals("success")){//判断是否成功
			//保存响应参数
			map.put("res", "1");//成功
			map.put("reason", data.get("reason").toString());
			Map<String, Object> results = new HashMap<String, Object>();
			results =  Utils.toMap(data.get("result").toString());
			map.put("bank", results.get("bank"));//银行归属
			map.put("type", results.get("type"));//卡类型
			map.put("tel", results.get("tel"));//客服电话
			map.put("logo", log_url +results.get("logo"));//银行logo
		}else{
			map.put("res", "2");
			map.put("reason", data.get("reason").toString());
		}
		return map;
	}
	
	/**
	 * 银行卡四元素校验
	 * @author lifuqiang
	 * 2017-2-17
	 * @param realname:姓名
	 * @param idcard：身份证号码
	 * @param bankcard：银行卡号
	 * @param mobile：银行预留电话号
	 * @return res:0：无法验证或不支持的银行；1：认证信息匹配；2：认证信息不匹配；3：请求次数不足；4：身份证号码填写错误；5：银行卡号码填写错误；6：服务器异常！
	 */
	public static Map<String, Object> verifybankcard4(String realname, String idcard, String bankcard, String mobile){
		Map<String, String> params = new HashMap<String, String>();//请求参数集合
		Map<String, Object> data = new HashMap<String, Object>();//响应参数集合
		Map<String, Object> map = new HashMap<String, Object>();//返回参数集合
		Map<String, Object> result = new HashMap<String, Object>();
		params.put("key", verifybankcard4_AppKey);//您申请的APPKEY
		params.put("realname", realname);//姓名，需要utf8 Urlencode
		params.put("idcard", idcard);//身份证号码
		params.put("bankcard", bankcard);//银行卡号
		params.put("mobile", mobile);//手机号码
		String results = HttpUtils.sendPost(verifybankcard4_url, params);
		if(log.isInfoEnabled()){log.info("verifybankcard4 string " + results);}
		data = Utils.toMap(results);
		if("221302".equals(data.get("error_code"))){
			map.put("res", "0");
			map.put("reason", "无法验证或不支持的银行");
		}else if("10012".equals(data.get("error_code"))){
			map.put("res", "3");
			map.put("reason","请求次数不足");
		}else if("221306".equals(data.get("error_code"))){
			map.put("res", "4");
			map.put("reason","身份证号码填写错误");
		}else if("221307".equals(data.get("error_code"))){
			map.put("res", "5");
			map.put("reason","银行卡号码填写错误");
		}else if("0".equals(data.get("error_code"))){
			result = Utils.toMap((String)data.get("result"));
			if("1".equals(result.get("res"))){
				map.put("res", "1");
				map.put("reason","认证信息匹配");
			}else if("2".equals(result.get("res"))){
				map.put("res", "2");
				map.put("reason","认证信息不匹配");
			}
		}else{
			map.put("res", "6");
			map.put("reason","服务器异常！");
		}
		return map;
	}
	
	/**
	 * 身份证实名认证
	 * @author lifuqiang
	 * 2017-2-20
	 * @param idcard：身份证号码
	 * @param realname：真实姓名
	 * @return
	 */
	// TODO 未接入，代码不完整
	public static Map<String, Object> idcardQuery(String idcard, String realname){
		Map<String, String> params = new HashMap<String, String>();//请求参数集合
		Map<String, Object> data = new HashMap<String, Object>();//响应参数集合
		Map<String, Object> map = new HashMap<String, Object>();//返回参数集合
		params.put("idcard", idcard);//身份证号码
		params.put("realname", realname);//姓名，需要utf8 Urlencode
		params.put("key", idcard_AppKey);//您申请的APPKEY
		String result = HttpUtils.get(HttpUtils.Parameter(idcard_url, params));
		data = Utils.toMap((String) Utils.toMap(result).get("result"));
		if(data.get("res").equals("1")){//判断是否匹配1:匹配
			//保存响应参数
			map.put("idcard", data.get("idcard"));//身份证号码
			map.put("realname", data.get("realname"));//姓名
			map.put("res", data.get("res").toString());//验证结果，1:匹配 2:不匹配
		}else if(data.get("res").equals("2")){//2:不匹配
			map.put("res", data.get("res").toString());
		}else if(data.isEmpty()){//当前可请求的次数不足
			map.put("res", "0");
			map.put("message", "当前可请求的次数不足");
		}
		return map;
	}
}
